describe("EppCsv", function() {
    describe("parse", function() {
        it("should be able to parse text", function() {
            var txt = 'a,b,c\nd,e,f\ng,h,i'
            var csv = EppCsv.parse(txt)

            expect(csv.length).toEqual(3);
            expect(csv[0].length).toEqual(3);
            expect(csv[1].length).toEqual(3);
            expect(csv[2].length).toEqual(3);
            expect(csv[0][0]).toEqual('a');
            expect(csv[0][1]).toEqual('b');
            expect(csv[0][2]).toEqual('c');
            expect(csv[1][0]).toEqual('d');
            expect(csv[1][1]).toEqual('e');
            expect(csv[1][2]).toEqual('f');
            expect(csv[2][0]).toEqual('g');
            expect(csv[2][1]).toEqual('h');
            expect(csv[2][2]).toEqual('i');
        });

        it("should be able to parse text with custom separator", function() {
            var txt = 'a\tb\tc\nd\te\tf\ng\th\ti'
            var csv = EppCsv.parse(txt, '\t')

            expect(csv.length).toEqual(3);
            expect(csv[0].length).toEqual(3);
            expect(csv[1].length).toEqual(3);
            expect(csv[2].length).toEqual(3);
            expect(csv[0][0]).toEqual('a');
            expect(csv[0][1]).toEqual('b');
            expect(csv[0][2]).toEqual('c');
            expect(csv[1][0]).toEqual('d');
            expect(csv[1][1]).toEqual('e');
            expect(csv[1][2]).toEqual('f');
            expect(csv[2][0]).toEqual('g');
            expect(csv[2][1]).toEqual('h');
            expect(csv[2][2]).toEqual('i');
        });

        it("should unescape parsed values", function() {
            var txt = '"a"\tb\t"c"\nd\te\tf\ng\th\ti'
            var csv = EppCsv.parse(txt, '\t')

            expect(csv.length).toEqual(3);
            expect(csv[0].length).toEqual(3);
            expect(csv[1].length).toEqual(3);
            expect(csv[2].length).toEqual(3);
            expect(csv[0][0]).toEqual('a');
            expect(csv[0][1]).toEqual('b');
            expect(csv[0][2]).toEqual('c');
            expect(csv[1][0]).toEqual('d');
            expect(csv[1][1]).toEqual('e');
            expect(csv[1][2]).toEqual('f');
            expect(csv[2][0]).toEqual('g');
            expect(csv[2][1]).toEqual('h');
            expect(csv[2][2]).toEqual('i');
        });
        it("should ignore empy rows", function() {
            var txt = 'a,b,c\n    \nd,e,f\ng,h,i\n\n\n'
            var csv = EppCsv.parse(txt)

            expect(csv.length).toEqual(3);
            expect(csv[0].length).toEqual(3);
            expect(csv[1].length).toEqual(3);
            expect(csv[2].length).toEqual(3);
            expect(csv[0][0]).toEqual('a');
            expect(csv[0][1]).toEqual('b');
            expect(csv[0][2]).toEqual('c');
            expect(csv[1][0]).toEqual('d');
            expect(csv[1][1]).toEqual('e');
            expect(csv[1][2]).toEqual('f');
            expect(csv[2][0]).toEqual('g');
            expect(csv[2][1]).toEqual('h');
            expect(csv[2][2]).toEqual('i');
        });
    });


    describe("parseObjects", function() {
        it("should be able to parse objects from text", function() {
            var txt = 'a,b,c\nd,e,f\ng,h,i'
            var objs = EppCsv.parseObjects(txt, ',', ['x', 'y', 'z'])

            expect(objs.length).toEqual(3);
            expect(objs[0].x).toEqual('a');
            expect(objs[0].y).toEqual('b');
            expect(objs[0].z).toEqual('c');
            expect(objs[1].x).toEqual('d');
            expect(objs[1].y).toEqual('e');
            expect(objs[1].z).toEqual('f');
            expect(objs[2].x).toEqual('g');
            expect(objs[2].y).toEqual('h');
            expect(objs[2].z).toEqual('i');
        });
        it("should be able to parse objects from text using header", function() {
            var txt = 'x,y,z\na,b,c\nd,e,f\ng,h,i'
            var objs = EppCsv.parseObjects(txt)

            expect(objs.length).toEqual(3);
            expect(objs[0].x).toEqual('a');
            expect(objs[0].y).toEqual('b');
            expect(objs[0].z).toEqual('c');
            expect(objs[1].x).toEqual('d');
            expect(objs[1].y).toEqual('e');
            expect(objs[1].z).toEqual('f');
            expect(objs[2].x).toEqual('g');
            expect(objs[2].y).toEqual('h');
            expect(objs[2].z).toEqual('i');
        });
    });

    describe("group", function() {
        it("should be able to group tabular data", function() {
            var data = [
                ['a', 1, 1],
                ['b', 2, 2],
                ['a', 3, 3],
                ['b', 4, 4]
            ];
            var groupFunc = function(rowa, rowb) {
                rowa[1] += rowb[1];
                return rowa;
            }
            var grouped = EppCsv.group(data, 0, groupFunc);

            expect(grouped.length).toEqual(2);
            expect(grouped[0][0]).toEqual('a');
            expect(grouped[1][0]).toEqual('b');
            //groupFunc soma col 1
            expect(grouped[0][1]).toEqual(4);
            expect(grouped[1][1]).toEqual(6);
            //groupFunc mantem col 2 do primeiro elemento
            expect(grouped[0][2]).toEqual(1);
            expect(grouped[1][2]).toEqual(2);
        });
    });
});
